"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GUC
Case Name   : 修改参数shared_buffers，观察预期结果；
Create At   : 2020/11/18
Owner       : opentestcase042
Description :
        1、查询shared_buffers默认值；
        2、修改shared_buffers为50MB，重启使其生效，并校验其预期结果；
        3、恢复默认值；
Expect      :
        1、显示默认值；
        2、参数修改成功，校验修改后系统参数值为50MB；
        3、恢复默认值成功；
History     :
        modified：2022-06-24 opentestcase045  用例中设置shared_buffers为2GB,
        容易因内存不足导致数据库重启失败，故修改为50MB
"""
import sys
import unittest
import os
sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH

class Deletaduit(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)}' + ' start-----')
        self.Constant = Constant()
        self.commonsh = CommonSH('dbuser')
    def test_startdb(self):
        text = '--step1:查询参数默认值;expect:显示成功--'
        self.log.info(text)
        sql_cmd = self.commonsh.execut_db_sql(f'''show shared_buffers;''')
        self.log.info(sql_cmd)
        self.res = sql_cmd.splitlines()[-2].strip()
        text = '--step2:使用设置gs_guc set设置shared_buffers;' \
               'expect:设置参数成功--'
        self.log.info(text)
        msg = self.commonsh.execute_gsguc('set',
                                          self.Constant.GSGUC_SUCCESS_MSG,
                                          'shared_buffers=50MB')
        self.log.info(msg)
        self.assertTrue(msg)
        msg = self.commonsh.restart_db_cluster()
        self.log.info(msg)
        self.assertTrue(msg)
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Normal" in status)
        text = '--step3:查询该参数修改后的值;expect:显示成功--'
        self.log.info(text)
        sql_cmd = self.commonsh.execut_db_sql(f'''show shared_buffers;''')
        self.log.info(sql_cmd)
        self.assertIn('50MB', sql_cmd)
    def tearDown(self):
        text = '--step4:清理环境;expect:清理环境完成--'
        self.log.info(text)
        sql_cmd = self.commonsh.execut_db_sql(f'''show shared_buffers;''')
        self.log.info(sql_cmd)
        if self.res not in sql_cmd:
            msg = self.commonsh.execute_gsguc('set',
                                              self.Constant.GSGUC_SUCCESS_MSG,
                                              f'''shared_buffers={self.res}''')
            self.log.info(msg)
            msg = self.commonsh.restart_db_cluster()
            self.log.info(msg)
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Normal" in status)
        self.log.info(f'-----{os.path.basename(__file__)}' + ' end-----')
